using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcDemo
{
    public class DataTypeValidator : DataAnnotationsModelValidator<DataTypeAttribute>
    {
        private readonly string message;

        public DataTypeValidator(ModelMetadata metadata, ControllerContext context, DataTypeAttribute attribute)
            : base(metadata, context, attribute)
        {
            message = attribute.ErrorMessage;
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rules = new List<ModelClientValidationRule>();
            ModelClientValidationRule rule;
            switch (Attribute.DataType)
            {
                case DataType.EmailAddress:
                    rule = new ModelClientValidationRule {ErrorMessage = message, ValidationType = "email"};
                    rule.ValidationParameters.Add("true", "true");
                    rules.Add(rule);
                    break;
                case DataType.Url:
                    rule = new ModelClientValidationRule {ErrorMessage = message, ValidationType = "url"};
                    rule.ValidationParameters.Add("true", "true");
                    rules.Add(rule);
                    break;
                //case DataType.Date:
                //    rule = new ModelClientValidationRule {ErrorMessage = message, ValidationType = "date"};
                //    rule.ValidationParameters.Add("true", "true");
                //    rules.Add(rule);
                //    break;
                //case DataType.DateTime:
                //    rule = new ModelClientValidationRule {ErrorMessage = message, ValidationType = "date"};
                //    rule.ValidationParameters.Add("true", "true");
                //    rules.Add(rule);
                //    break;
            }
            return rules;
        }
    }
}